package com.bw.services

import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.bw.common.utils.LogUtil
import com.bw.lib_base.base.BaseActivity
import com.bw.services.adapter.Car_ManageAdapter
import com.bw.services.databinding.ActivityServicesBinding
import com.bw.services.entity.CarManage
import com.bw.services.viewmodel.ServicesViewModel
import com.therouter.router.Route
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import android.view.inputmethod.EditorInfo
import com.therouter.TheRouter

@AndroidEntryPoint
@Route(path = "app/ServicesActivity")
class ServicesActivity : BaseActivity<ActivityServicesBinding,ServicesViewModel>() {
  lateinit var carAdapter: Car_ManageAdapter
   private var list = mutableListOf<CarManage>()
   private var currentSearch: String = "京AB876"
    override fun generatePremission(pageName: String): Array<String> {
        return this@ServicesActivity.generatePremission(pageName)
    }

    override fun setupUI() {
        binding.carManage.apply {
            carAdapter = Car_ManageAdapter(list)
            binding.carManage.adapter = carAdapter
            layoutManager =LinearLayoutManager(this@ServicesActivity)
        }
        binding.ivBack.setOnClickListener { finish() }
    }

    override fun fetchData() {
        vm.getServices(currentSearch)
    }

    override fun observeData() {
        lifecycleScope.launch {
            vm.serviceData.collect{
                LogUtil.d("car_manage",it.toString())
                if(it!=null){
                    list.clear()
                    list.addAll(listOf(it))
                    carAdapter.notifyDataSetChanged()
                    //car_manage_adapter.notifyDataSetChanged()
                }
            }
        }
//        lifecycleScope.launch {
//            vm.carParking.collect{
//                LogUtil.d("car_parking",it.toString())
//            }
//        }

        binding.add.setOnClickListener {
            TheRouter.build("services/AddcarActivity")
                .navigation(this)
        }
    }

    override fun handleEvent() {

    }

    override val mLayoutResId: Int
        get() = R.layout.activity_services
}